//
//  EventsAnnouncedVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/22.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class EventsAnnouncedVC: BaseViewController {
    
    var page = -999
    
    var goodsId = "-999"

    var hasmore = "-999"
    
    var lastPblish : LastPblishModel?
    
    var dataModels : [SurpriseShopDetModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "往期揭晓"
        
        plainTableView.tableFooterView = UIView()
        
        plainTableView.backgroundColor = UIColor.viewBgGrey()
        
//        plainTableView.separatorStyle = .none
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: EveAnnedHeaderView.self), bundle: nil), forHeaderFooterViewReuseIdentifier: EveAnnedHeaderView.identifer())
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: EventAnnouncedCell.self), bundle: nil), forCellReuseIdentifier: EventAnnouncedCell.identifer())
        
        weak var weakSelf = self
        
        plainTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadData()
            
        }
        
        plainTableView.mj_header.beginRefreshing()
        
        plainTableView.mj_footer = ZMRefreshFooter.init{
            
            weakSelf!.loadMoreData()
            
        }
        
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

  
}


extension EventsAnnouncedVC{

    func loadData(){
        
        page = 1
        
        var parmeter  = [String : AnyObject]()
        
        parmeter["curpage"] = "\(page)" as AnyObject
        
        parmeter["goods_id"] = goodsId as AnyObject
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=one_dollar&a=history_publish_list", parameters: parmeter) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                self?.hasmore = responseObject!["hasmore"] as! String
                
                
                self?.dataModels = [SurpriseShopDetModel]()
                
                
                if let temp = responseObject!["lastest_publish"]{
                
                self?.lastPblish = LastPblishModel(dic: temp as! [String : AnyObject])
                
                }
                
//                self?.lastPblish = LastPblishModel(dic: ["od_id" : "31" as AnyObject,"od_no" : "170809100" as AnyObject])
                
                
                for dic in responseObject!["datas"]!["list"] as! [[String : AnyObject]]{
                    
                    self!.dataModels!.append(SurpriseShopDetModel(dic: dic))
                    
                }
                
                self!.plainTableView.mj_footer.resetNoMoreData()
                
                
                if self?.hasmore == "0" {
                    
                    self!.plainTableView.mj_footer.endRefreshingWithNoMoreData()
                    
                }
                
                
                if self!.dataModels!.count == 0{
                    
                    self?.showLogoView(text : "暂无揭晓商品...")
                    
                }
                
                self!.plainTableView.mj_header.endRefreshing()
                
                self!.plainTableView.reloadData()
                
                
            }else{
                
                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
            }
            
            
        }
        
    }
    
    
    
    
    func loadMoreData(){
        
        page += 1
        
        var parmeter  = [String : AnyObject]()
        
        parmeter["curpage"] = "\(page)" as AnyObject
        
        parmeter["goods_id"] = goodsId as AnyObject
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=one_dollar&a=history_publish_list", parameters: parmeter) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                self?.hasmore = responseObject!["hasmore"] as! String
                
                
                for dic in responseObject!["datas"]!["list"] as! [[String : AnyObject]]{
                    
                    self!.dataModels!.append(SurpriseShopDetModel(dic: dic))
                    
                }
                
                if self?.hasmore == "0" {
                    
                    self!.plainTableView.mj_footer.endRefreshingWithNoMoreData()
                    
                }else{
                    
                    self!.plainTableView.mj_footer.endRefreshing()
                    
                }
                
                self!.plainTableView.reloadData()
                
            }else{
                
                self!.plainTableView.mj_footer.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
        }
        
        
        
    }
    

    
}




extension EventsAnnouncedVC{
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return 1
        
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return dataModels?.count ?? 0
        
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let temp = tableView.dequeueReusableCell(withIdentifier: EventAnnouncedCell.identifer()) as! EventAnnouncedCell
        
        temp.model = dataModels?[indexPath.row]
        
        return temp
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 150
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
//        let temp = SurpriseShopDetailVC()
//        
//        
//        temp.odId = dataModels![indexPath.row].od_id
//        
//        
//        navigationController?.pushViewController(temp, animated: true)
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if lastPblish == nil {
            
            return 0
            
        }else{
            
            return 36
        }
        
    }
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        
        if lastPblish == nil {
            
            return UIView()
            
        }else{
            
            let temp = tableView.dequeueReusableHeaderFooterView(withIdentifier: EveAnnedHeaderView.identifer()) as! EveAnnedHeaderView
            
            temp.model = lastPblish
            
            return temp
            
        }

        
     
    }
    
    
    
}








